﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Video;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class CameraTitle : MonoBehaviour
{
    [Header("基础配置")]
    [Tooltip("片头影片播放对象")]
    public GameObject moviePanelObject;
    [Tooltip("主画面对象")]
    public GameObject mainPanelObject;
    [Tooltip("1P文字选项")]
    public GameObject player1Object;
    [Tooltip("2P文字选项")]
    public GameObject player2Object;
    [Tooltip("选择标记")]
    public GameObject selectFlagObject;
    [Tooltip("标题音乐")]
    public AudioSource titleMusic;
    [Tooltip("1P、2P对应选择标记的位置")]
    public Vector2[] selectFlagPosition = new Vector2[]
    {
        new Vector2(-84f,-44f),
        new Vector2(-84f,-44f-16f),
    };
    [Tooltip("画面淡入淡出所需的总帧数")]
    public int fadeFinishedFrame = 58;
    [Tooltip("每秒显示帧数")]
    public int framePreSeconds = 60;
    [Tooltip("淡入淡出对象集合")]
    private FadeInOut[] fadeInOutArray;

    [Header("动作定义")]
    [Tooltip("画面淡入动作定义")]
    public SeqAction fadeInAction;
    [Tooltip("画面淡出动作定义")]
    public SeqAction fadeOutAction;
    [Tooltip("选择确定后文字闪烁动作定义")]
    public SeqAction selectionFlashAction;


    [Header("操作状态")]
    [Tooltip("选择键是否按下")]
    private bool selectPress;
    [Tooltip("开始键是否按下")]
    private bool startPress;
    [Tooltip("游戏是否确定开始")]
    private bool startGame;

    private void Awake()
    {
        moviePanelObject.SetActive(false);
        mainPanelObject.SetActive(true);
        fadeInOutArray = FindObjectsOfType<FadeInOut>();
        selectFlagObject.SetActive(false);

    }


    private void Update()
    {
        if (fadeInAction.done)
        {
            selectPress |= Input.GetButtonDown("Select");
            startPress |= Input.GetButtonDown("Start");
        }

    }
    private void FixedUpdate()
    {
        if (!fadeInAction.done)
        {
            //界面淡入
            fadeInAction.Update(FadeInAction);
        }
        else if (!startGame)
        {
            //按选择键标记变换位置
            UpdateSelect();
        }
        else if (!selectionFlashAction.done)
        {
            //选择后文字闪烁
            selectionFlashAction.Update(SelectionFlashAction);
        }
        else if (!fadeOutAction.done)
        {
            new GameObject[] { player1Object, player2Object }.Action((GameObject o) =>
            {
                if (o.TryGetComponent(out Text text))
                {
                    text.enabled = true;
                };
            });
            //界面淡出
            fadeOutAction.Update(FadeOutAction);
        }
        else
        {
            UpdateSkipMovie();
        }

        selectPress = false;
        startPress = false;
    }

    private void GoScense()
    {
        moviePanelObject.SetActive(false);
        ContraGlobal.ResetLife();
        SceneManager.LoadScene(ContraGlobal.SceneName_Map, LoadSceneMode.Single);
    }

    private void PlayStartMovie()
    {
        mainPanelObject.SetActive(false);
        moviePanelObject.SetActive(true);
        if (moviePanelObject.TryGetComponent(out VideoPlayer vplayer))
        {
            vplayer.loopPointReached += (VideoPlayer source) =>
            {
                GoScense();
            };
            vplayer.prepareCompleted += (VideoPlayer source) =>
            {
                source.Play();
                if (moviePanelObject.TryGetComponent(out RawImage rawImage))
                {
                    rawImage.enabled = true;
                }
            };
            vplayer.Prepare();
        }
        if (moviePanelObject.TryGetComponent(out RawImage rawImage))
        {
            rawImage.enabled = false;
        }
    }
    private void UpdateSkipMovie()
    {
        if (startPress)
        {
            GoScense();
        }
    }

    private void UpdateSelect()
    {
        selectFlagObject.SetActive(true);
        if (selectPress)
        {
            ContraGlobal.SetTwoPlayers(!ContraGlobal.IsTwoPlayers());
        }
        if (startPress)
        {
            startGame = true;
        }
        Vector2 flagPosition = selectFlagPosition[ContraGlobal.IsTwoPlayers() ? 1 : 0];
        if (selectFlagObject.TryGetComponent(out RectTransform rt))
        {
            rt.anchoredPosition = flagPosition;
        }


    }
    private void FadeInAction(SeqAction seq)
    {
        fadeInOutArray.Action((FadeInOut fadeInOut) =>
        {
            fadeInOut.UpdateFadeIn(seq.DurationTime, framePreSeconds);
        });
        if (seq.DurationTime >= (float)fadeFinishedFrame / (float)framePreSeconds)
        {
            seq.done = true;
            titleMusic.Play();
        }
    }

    private void FadeOutAction(SeqAction seq)
    {
        if (seq.IsFirstAction())
        {
            fadeInOutArray.Action((FadeInOut fadeInOut) =>
            {
                fadeInOut.Reset();
            });
        }

        fadeInOutArray.Action((FadeInOut fadeInOut) =>
        {
            fadeInOut.UpdateFadeOut(seq.DurationTime, framePreSeconds);
        });
        if (seq.DurationTime >= (float)fadeFinishedFrame / (float)framePreSeconds)
        {
            fadeOutAction.done = true;
            PlayStartMovie();
        }
    }

    private void SelectionFlashAction(SeqAction seq)
    {
        GameObject playerObject = ContraGlobal.IsTwoPlayers() ? player2Object : player1Object;
        if (playerObject.TryGetComponent(out Text text))
        {
            text.enabled = seq.seqNo % 2 != 0;
        }
    }



}
